import $http from './httpRequest'

export function getOfficeName (ids, defaultName) {
  if ((!ids && ids !== 0)) {
    if (defaultName !== undefined) {
      return defaultName
    } else {
      return '--'
    }
  }
  let officeCacheList = JSON.parse(localStorage.getItem('officeList') || '[]')
  let officeName
  // 将部门id字符串通过','分割为单独的部门id
  ids.split(',').forEach(function (id) {
    // 通过部门id获取部门名称赋值给部门名称字符串
    if (officeName === undefined) {
      officeName = officeCacheList[id]
    } else {
      officeName += ',' + officeCacheList[id]
    }
  })
  if (officeName) {
    return officeName
  }
}

export function refreshOfficeCacheList () {
  $http.get('/sys/office/getOfficeCacheList').then(({data}) => {
    localStorage.setItem('officeList', JSON.stringify(data.officeList || '[]'))
  })
}

export default {getOfficeName, refreshOfficeCacheList}
